<?php
class CommonModel extends Model {

     protected $_auto = array(
        array('create_time','time',1,'function'),
        array('update_time','time',3,'function'),
    );

	// 获取当前用户的ID
    public function getMemberId() {
        return isset($_SESSION[C('USER_AUTH_KEY')])?$_SESSION[C('USER_AUTH_KEY')]:0;
    }

   /**
     +----------------------------------------------------------
     * 根据条件禁用表数据
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $options 条件
     +----------------------------------------------------------
     * @return boolen
     +----------------------------------------------------------
     */
    public function forbid($options,$field='status'){

        if(FALSE === $this->where($options)->setField($field,0)){
            $this->error =  L('_OPERATION_WRONG_');
            return false;
        }else {
            return True;
        }
    }

	 /**
     +----------------------------------------------------------
     * 根据条件批准表数据
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $options 条件
     +----------------------------------------------------------
     * @return boolen
     +----------------------------------------------------------
     */

    public function checkPass($options,$field='status'){
        if(FALSE === $this->where($options)->setField($field,1)){
            $this->error =  L('_OPERATION_WRONG_');
            return false;
        }else {
            return True;
        }
    }


    /**
     +----------------------------------------------------------
     * 根据条件恢复表数据
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $options 条件
     +----------------------------------------------------------
     * @return boolen
     +----------------------------------------------------------
     */
    public function resume($options,$field='status'){
        if(FALSE === $this->where($options)->setField($field,1)){
            $this->error =  L('_OPERATION_WRONG_');
            return false;
        }else {
            return True;
        }
    }

    /**
     +----------------------------------------------------------
     * 根据条件恢复表数据
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $options 条件
     +----------------------------------------------------------
     * @return boolen
     +----------------------------------------------------------
     */
    public function recycle($options,$field='status'){
        if(FALSE === $this->where($options)->setField($field,0)){
            $this->error =  L('_OPERATION_WRONG_');
            return false;
        }else {
            return True;
        }
    }

    public function recommend($options,$field='is_recommend'){
        if(FALSE === $this->where($options)->setField($field,1)){
            $this->error =  L('_OPERATION_WRONG_');
            return false;
        }else {
            return True;
        }
    }

    public function unrecommend($options,$field='is_recommend'){
        if(FALSE === $this->where($options)->setField($field,0)){
            $this->error =  L('_OPERATION_WRONG_');
            return false;
        }else {
            return True;
        }
    }

     /**
     +----------------------------------------------------------
     * 根据条件给表的某个字段设置新值
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $options 条件
     +----------------------------------------------------------
     * @return boolen
     +----------------------------------------------------------
     */
    public function setFieldValue($options,$newValue=0,$field='status'){

        if(FALSE === $this->where($options)->setField($field,$newValue)){
            $this->error =  L('_OPERATION_WRONG_');
            return false;
        }else {
            return True;
        }
    }
    
    //生成四位数字编码
    public function build_no($no){
        if($no)
           return $no;
        $max_no = $this->order('id desc')->getField('mano');
         return sprintf("%04d", $max_no+1);  
    
    }
        /**注意type首字母大写
     *生成单号如 RK20110528-001 ，前几位为当前单子的日期，后三位自动增长
     */
    public function genManNo($type='Enter'){
        $currday = date('Ymd');
        $prefix = '';

        switch ($type){
            case 'Buy'://采购计划
                $prefix = 'CG';
                break;
            case 'Enter'://入库
                $prefix = 'RK';
                break;
            case 'Back_supplier'://退货给供应商
                $prefix = 'TH';
                break;
            case 'Broken'://报废
                $prefix = 'BF';
                break;
            case 'Sale'://销售
                $prefix = 'XS';
                break;
            case 'Sale_back'://客户退货
                $prefix = 'TD';
                break;
            case 'Switch'://调拨
                $prefix = 'DB';
                break;
            case 'Wholesale'://批发
                $prefix = 'PF';
                break;
            default:
                 $prefix = 'RK';
        }

        $model = M($type);
            $lastNo = $model ->where(strtolower($type).'_no like \'%'.$currday.'-%\'')->order('id desc')->field(strtolower($type).'_no')->find();
            $value = explode('-',$lastNo[strtolower($type).'_no'] );


        if (empty($value)) {
            return $no = $prefix . $currday . '-001';
        } else {
            if (substr($value['0'], 2) == $currday) {
                $value['1'] += 1;
                $value['1'] = sprintf("%03d", $value['1']); //格式化为001
            } else {
                $value['1'] = '001';
            }
            return $no = $prefix . $currday . '-' . $value['1'];
        }
    }


    public function intime($strtime){
    	return empty($strtime)?0:strtotime($strtime);
    }
    /**
     * Create changes of one object.
     *
     * @param mixed $old    the old object
     * @param mixed $new    the new object
     * @static
     * @access public
     * @return array
     */
    public static function createChanges($old, $new)
    {
        global $config;
        $changes    = array();
        $magicQuote = get_magic_quotes_gpc();
        foreach($new as $key => $value)
        {
            //TODO 不需要比较的字段
            if(strtolower($key) == 'update_time') continue;
            if(strtolower($key) == 'create_time')   continue;
            if(strtolower($key) == 'assigneddate')   continue;
            if(strtolower($key) == 'editedby')       continue;
            if(strtolower($key) == 'editeddate')     continue;

            //diff比较的是varchar类的字段
            if($magicQuote) $value = stripslashes($value);
            if($value != $old[$key])
            {
                $diff = '';
                if(substr_count($value, "\n") > 1
                        or substr_count($old[$key], "\n") > 1
                            or strpos('name,remark,content,title', strtolower($key)) !== false)
                     $diff = self::diff($old[$key], $value);
                $changes[] = array('field' => $key, 'old' => $old[$key], 'new' => $value, 'diff' => $diff);
            }
        }
        return $changes;
    }

    /**
     * Diff two string. (see phpt)
     *
     * @param string $text1
     * @param string $text2
     * @static
     * @access public
     * @return string
     */
    public static function diff($text1, $text2)
    {
        $text1 = str_replace('&nbsp;', '', trim($text1));
        $text2 = str_replace('&nbsp;', '', trim($text2));
        $w  = explode("\n", $text1);
        $o  = explode("\n", $text2);
        $w1 = array_diff_assoc($w,$o);
        $o1 = array_diff_assoc($o,$w);
        $w2 = array();
        $o2 = array();
        foreach($w1 as $idx => $val) $w2[sprintf("%03d<",$idx)] = sprintf("%03d- ", $idx+1) . "<del>" . trim($val) . "</del>";
        foreach($o1 as $idx => $val) $o2[sprintf("%03d>",$idx)] = sprintf("%03d+ ", $idx+1) . "<ins>" . trim($val) . "</ins>";
        $diff = array_merge($w2, $o2);
        ksort($diff);
        return implode("\n", $diff);
    }

}
?>
